package com.itbaizhan.dubbo;

import com.itbaizhan.common.BaseResponse;
import com.itbaizhan.entity.TOrder;
import com.itbaizhan.mapper.TOrderMapper;
import com.itbaizhan.service.ITAccountService;
import com.itbaizhan.service.ITOrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 订单业务层
 */
@DubboService
public class TOrderServiceImpl implements ITOrderService {


    @Autowired
    private TOrderMapper tOrderMapper;


    @DubboReference
    private ITAccountService itAccountService;


    @Override
    public BaseResponse save(String userId, String commodityCode, Integer count, Integer money) {
        // 1、账户扣减
        itAccountService.deduct(userId,money);
        System.out.println(10/0);
        // 2、创建订单
        TOrder tOrder = new TOrder();
        tOrder.setUserId(userId);
        tOrder.setCommodityCode(commodityCode);
        tOrder.setCount(count);
        tOrder.setMoney(money);
        // 3、保存订单
        int insert = tOrderMapper.insert(tOrder);
        if (insert > 0){
            return BaseResponse.success();
        }
        return BaseResponse.error("服务异常");
    }


}
